// LoadingManager.kt
package com.example.k12.http

import android.app.Dialog
import android.content.Context
import android.view.ViewGroup
import android.widget.ProgressBar

/**
 * 全局加载对话框管理器
 */
object LoadingManager {
    private var loadingDialog: Dialog? = null

    /**
     * 显示加载对话框
     * @param context 上下文
     * @param message 提示信息
     */
    fun showLoading(context: Context?, message: String = "加载中...") {
        if (context == null) return
        
        hideLoading()
        
        loadingDialog = Dialog(context).apply {
            setContentView(ProgressBar(context).apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
            })
            setCancelable(false)
            setCanceledOnTouchOutside(false)
            window?.setBackgroundDrawableResource(android.R.color.transparent)
        }
        
        loadingDialog?.show()
    }
    
    /**
     * 隐藏加载对话框
     */
    fun hideLoading() {
        loadingDialog?.dismiss()
        loadingDialog = null
    }
}
